﻿using System.ComponentModel;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.K3.SCM.App.Purchase.Report;

namespace Caraya.MFG.BusinessPlugIn
{
    [Description("二开采购订单执行明细"), HotUpdate]
    public class PurchaseOrderExecuteRptEx : PurchaseOrderExecuteRpt
    {
        private string[] customTableNames;
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            IDBService dbService = Kingdee.BOS.App.ServiceHelper.GetService<IDBService>();
            customTableNames = dbService.CreateTemporaryTableName(this.Context, 1);
            string strTable = customTableNames[0];
            base.BuilderReportSqlAndTempTable(filter, strTable);
            /// 扩展 物料描述字段
            string text = @"/*dialect*/
SELECT t1.* ,e0.FDescription
into {0}
FROM {1} t1
INNER JOIN
    (SELECT   t0.FMATERIALID, t0.FNUMBER AS FNumber, t0_L.FSPECIFICATION AS FSpecification, 
                        t0_L.FNAME AS FName,t0_L.FDESCRIPTION AS FDescription
        FROM      T_BD_MATERIAL AS t0 LEFT OUTER JOIN
                        T_BD_MATERIAL_L AS t0_L ON t0.FMATERIALID = t0_L.FMATERIALID AND t0_L.FLOCALEID = 2052) AS e0
ON t1.FMaterialId = e0.FMATERIALID
";
            var sql = string.Format(text, tableName, strTable);
            DBServiceHelper.ExecuteDynamicObject(this.Context, sql);
        }

        public override void CloseReport()
        {
            if (customTableNames.IsNullOrEmptyOrWhiteSpace())
            {
                return;
            }
            IDBService dbService = Kingdee.BOS.App.ServiceHelper.GetService<IDBService>();
            dbService.DeleteTemporaryTableName(this.Context, customTableNames);
            base.CloseReport();
        }
    }
}
